package com.web.system.scheduler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.web.notice.service.NoticeService;
import com.web.order.service.OrderService;
import com.web.system.service.UserService;

/**
 * 定时器
 * @author g-gaojp
 * @date 2016-9-5
 */
@Component
public class TaskScheduler {

	@Autowired
	private UserService userService;
	
	@Autowired
	private NoticeService noticeService;
	
	@Autowired
	private OrderService orderService;
	
	/**
	 * 每分钟执行一次，判断订单状态是否过期
	 * @throws Exception
	 */
	@Scheduled(cron="0 * * * * *") 
	public void changeOrderState() { 
		try {
			orderService.schedulerUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 每天晚上12点执行一次，注销12个月以上没有登录过的用户
	 * @throws Exception
	 */
	@Scheduled(cron="0 0 12 * * ?") 
	public void cancleUser() {
		try {
			userService.cancleUser("12");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	/**
	 * 每小时执行一次，根据期限来改变公告的状态
	 * @throws Exception
	 */
	@Scheduled(cron="0 0 */1 * * ?")
	public void changeNoticeState(){
		try {
			noticeService.schedulerUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	/**
	 * 每小时执行一次，根据期限来改变房间的状态（订单状态本身是已经成功支付 的订单）
	 * @throws Exception
	 */
	@Scheduled(cron="0 0 */1 * * ?")
	public void clearRoomToday(){
		try {
			orderService.clearRoomToday();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	/**
	 * 每天晚上12点执行一次，清理昨天过期限的房间的状态
	 * @throws Exception
	 */
	@Scheduled(cron="0 0 12 * * ?") 
	public void clearRoomYesterday(){
		try {
			orderService.clearRoomYesterday();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
